package com.sgcc.fj.pmos.mobileapp.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.arouter.app.AppArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.fj.pmos.common.utils.BurialPointUtils;
import com.sgcc.fj.pmos.common.utils.ImmersionBarUtils;
import com.sgcc.fj.pmos.common.utils.VisibilConfig;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.mobileapp.bean.ConfigBean;
import com.sgcc.fj.pmos.mobileapp.contract.SplashContract;
import com.sgcc.fj.pmos.mobileapp.presenter.SplashPresenter;

public class SplashActivity extends MvpBaseActivity<SplashPresenter> implements SplashContract.View {

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_splash;
    }

    @Override
    protected SplashPresenter createPresenter() {
        return new SplashPresenter();
    }

    @Override
    protected boolean isCustomCreate() {
        return true;
    }

    @Override
    protected void customInit(Bundle savedInstanceState) {
        super.customInit(savedInstanceState);
        setContentView(getContentViewLayoutID());
        BurialPointUtils.onBurialPoint(BurialPointUtils.mPoint.APP_OPEN);
        ImmersionBarUtils.initbar(this);
        if (!this.isTaskRoot()) {
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                finish();
                return;
            }
        }
//        mPresenter.getVisibilConfig();
        SPUtils.getInstance("SignText").put("Authorization", "");//清除保存的token
        handler.sendEmptyMessageDelayed(2, 500);

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.splash_animation_in, R.anim.splash_animation_out);
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case 2:
                    try {
                        ARouter.getInstance()
                                .build(AppArouterPath.APP_MAIN)
//                                .build(PersonalCenterArouterPath.PERSONALCENTER_SETPOWERRATIO)
                                .navigation();
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    public void queryVisibilConfigSuccess(ConfigBean response) {
        if (response != null) {
            if (response.getCode() == 200) {
                if (response.getData() != null && response.getData().size() != 0) {
                    for (int i = 0; i < response.getData().size(); i++) {
                        if (TextUtils.equals("isAllowLogin", response.getData().get(i).getParamId())) {
                            if (TextUtils.equals("0", response.getData().get(i).getParamValue())) {
                                VisibilConfig.setUserCanLog(false);
                            } else if (TextUtils.equals("1", response.getData().get(i).getParamValue())) {
                                VisibilConfig.setUserCanLog(true);
                            }
                        }
//                        if (TextUtils.equals("isContentHiding", response.getData().get(i).getParamId())) {
//                            if (TextUtils.equals("0", response.getData().get(i).getParamValue())) {
//                                VisibilConfig.setShowVisibil(false);
//                            } else if (TextUtils.equals("1", response.getData().get(i).getParamValue())) {
//                                VisibilConfig.setShowVisibil(true);
//                            }
//                        }
                    }
                }
            } else {
                ToastUtils.showShort(response.getMsg());
            }
//            if(TextUtils.equals("1",response.getIsContentHiding())){
//                VisibilConfig.setShowVisibil(false);
//            }else if(TextUtils.equals("0",response.getIsContentHiding())){
//                VisibilConfig.setShowVisibil(true);
//            }
//
//            if(TextUtils.equals("1",response.getIsAllowLogin())){
//                VisibilConfig.setUserCanLog(false);
//            }else if(TextUtils.equals("0",response.getIsAllowLogin())){
//                VisibilConfig.setUserCanLog(true);
//            }
        } else {
            VisibilConfig.setUserCanLog(true);
            VisibilConfig.setShowVisibil(true);
        }
        handler.sendEmptyMessageDelayed(2, 500);
    }

    @Override
    public void queryVisibilConfigFailed() {
        ToastUtils.showShort("配置文件获取失败");
        handler.sendEmptyMessageDelayed(2, 500);
    }
}
